<?php echo view('header.html');?>

<body class="layui-view-body">
    <div class="layui-content">
        <div class="layui-row">
            <div class="layui-card">
                <div class="layui-card-header">
                    导航分类管理
                </div>
                <div class="layui-card-body">
                    <table id="listTable" lay-filter="listTable"></table>
                    <script type="text/html" id="toolbarOption">
                        <button type="button" lay-event="del" title="您确定要这样操作吗？" class="layui-btn layui-btn-danger layui-btn-sm"> 删除</button>
                        <button class="layui-btn layui-btn-blue layui-btn-sm addType" type="button">添加导航分类</button>
                    </script>

                    <script type="text/html" id="statusTpl">
                        <input type="checkbox" name="status" value="{{d.status}}" lay-skin="switch" lay-text="显示|不显示" lay-filter="statusSwitch" {{ d.status == 1 ? 'checked' : '' }}>
                    </script>

                    <script type="text/html" id="barOption">
                        <a class="layui-btn layui-btn-xs layui-btn-warm" lay-event="edit">修改</a>
                        <a class="layui-btn layui-btn-xs layui-btn-default" lay-event="add">添加{{d.name}}菜单</a>
                        <a class="layui-btn layui-btn-xs rowList" lay-event="list">菜单列表</a>
                        <a class="layui-btn layui-btn-xs layui-btn-danger rowClear" lay-event="clear">清空数据</a>
                        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
                    </script>
                </div>
            </div>
        </div>
    </div>

    <?php echo view('footer.html');?>
    <script type="text/javascript">
        layui.use(['form', 'jquery', 'layer', 'table'], function () {
            var $ = layui.jquery,
                form = layui.form,
                layer = layui.layer,
                table = layui.table;
            
            /**index**/

            //数据初始化
            var tableInit = table.render({
                elem: '#listTable',
                toolbar: "#toolbarOption",
                defaultToolbar: [],
                title: '导航分类列表',
                url: adminpath + 'admin/navbar/index',
                method: 'post',
                cols: [
                    [
                        { type: 'checkbox', width: 50 },
                        { field: 'id', title: 'ID', width: 80, unresize: true, sort: true },
                        { field: 'name', width: 200, title: '分类名称' },
                        { fixed: 'right', title: '操作', fixed: 'right', toolbar: '#barOption' }
                    ]
                ],
                page: {
                    layout: ['prev', 'page', 'next', 'count'],
                    groups: 5,
                    first: true,
                    last: true
                },
                response: {
                    statusCode: 0,
                    countName: 'count'
                },
                parseData: function (res) {
                    return {
                        'code': res.code,
                        "data": res.data.data,
                        'count': res.data.total
                    };
                }
            });

            //表头工具栏
            table.on('toolbar(listTable)', function (obj) {
                var checkStatus = table.checkStatus(obj.config.id);
                var data = checkStatus.data;
                var ids = [];
                $.each(data, function (i, v) {
                    ids.push(v.id);
                })

                if (ids.length < 1) {
                    layer.msg('您还没有选择', { icon: 5, anim: 6 });
                    return false;
                }

                switch (obj.event) {

                    case 'del':
                        layer.confirm('确定要删除这些？', function (index) {
                            $.post(adminpath + 'admin/navbar/delType', { ids: ids }, function (res) {
                                if (res.code == 0) {
                                    layer.msg('操作成功', { icon: 1, time: 1500 });
                                } else {
                                    layer.msg(res.msg, { icon: 5, anim: 6 });
                                }
                                tableInit.reload();
                            }, 'json');
                            layer.close(index);
                        });
                        break;

                };
            });

            //监听表格行操作
            table.on('tool(listTable)', function (obj) {
                var data = obj.data;
                var id = data.id
                switch (obj.event) {
                    case 'edit':

                        form.val('formType', {
                            'data[id]': data.id,
                            'data[name]': data.name
                        });

                        layer.open({
                            type: 1,
                            area: ['600px', '500px'],
                            title: '修改分类',
                            content: $("#addType")
                        });
                        break;
                    case 'add':
                        var title = '添加[' + data.name + ']菜单：';
                        var url = adminpath + 'admin/navbar/add?tid=' + id;
                        layer.open({
                            type: 2,
                            area: ['600px', '90%'],
                            title: title,//false
                            content: [url]
                        });
                        break;
                    case 'list':
                        //数据列表
                        var name = data.name;
                        var tid = data.id;
                        var title = '[' + name + '(' + tid + ')]列表';
                        var url = adminpath + 'admin/navbar/list?tid=' + tid;
                        open_parent_tab(title, url);
                        break;
                    case 'clear':
                        var name = data.name;
                        var tid = data.id;
                        var url = adminpath + 'admin/navbar/clear';
                        layer.confirm('确定要清除数据吗？', function (index) {
                            $.post(url, { tid: tid, name: name }, function (res) {
                                if (res.code == 0) {
                                    layer.msg(res.msg, { icon: 1, time: 1500 }, function () {
                                        window.location.reload();
                                    });
                                } else {
                                    layer.msg(res.msg, { icon: 5, anim: 6 });
                                }

                            }, 'json');
                            layer.close(index);
                        });
                        break;

                    case 'del':
                        layer.confirm('确定要删除吗？', function (index) {
                            $.post(adminpath + 'admin/navbar/delType', { ids: [id] }, function (res) {
                                if (res.code == 0) {
                                    obj.del();
                                    layer.msg(res.msg, { icon: 1, time: 1500 }, function () {
                                        tableInit.reload();
                                    });
                                } else {
                                    layer.msg(res.msg, { icon: 5, anim: 6, time: 1500 }, function () {
                                        tableInit.reload();
                                    });
                                }
                            }, 'json');
                        });
                        break;
                }
            });


            //监听状态操作
            form.on('switch(statusSwitch)', function (obj) {
                var id = obj.othis.parents('tr').find("td:eq(1)").find(".layui-table-cell").text();
                var title = '';
                var val = '';
                if (this.value == 1) {
                    title = '您确定要[禁用]它吗?';
                    val = 0;
                } else {
                    title = '您确定要[启用]它吗?';
                    val = 1;
                }

                //点确认再切换
                var x = obj.elem.checked;
                obj.elem.checked = !x;
                form.render('checkbox');

                layer.confirm(title, function (index) {
                    $.post(adminpath + "admin/navbar/switchStatus", { id: id, value: val }, function (res) {
                        if (res.code == 0) {
                            layer.msg('操作成功', { icon: 1, time: 1500 });
                        } else {
                            layer.msg(res.msg, { icon: 5, anim: 6 });
                        }
                        tableInit.reload();
                    }, 'json');
                    layer.close(index);
                });
            });


            //搜索条件
            form.on('submit(ams_reset)', function (data) {
                $("#searchForm")[0].reset();
                form.render();
                $('#ams_search').trigger('click');
            });

            //标题搜索
            form.on('submit(ams_search)', function (data) {
                tableInit.reload({
                    where: data.field
                    , page: {
                        curr: 1
                    }
                });
            });


            //添加分类
            $("body").on('click', '.addType', function () {
                layer.open({
                    type: 1,
                    area: ['600px', '500px'],
                    title: '添加分类',
                    content: $("#addType")
                });
            });

            //提交分类
            form.on('submit(typeSubmit)', function (data) {
                var layindex = layer.load(2, { time: 8000 });
                var id = $("#typeid").val();
                var url = adminpath + 'admin/navbar/addType';
                if (id) {
                    url = adminpath + 'admin/navbar/editType?id=' + id;
                }
                $.post(url, data.field, function (res) {
                    layer.close(layindex);
                    if (res.code == 0) {
                        layer.msg(res.msg, { icon: 1, time: 1500 }, function () {
                            window.location.reload();
                        });
                    } else {
                        layer.msg(res.msg, { icon: 5, time: 1500 });
                    }
                }, 'json');
            });

        })
    </script>


    <!--添加修改分类-->
    <div class="hide" id="addType" style="display: none; margin-top: 30px;">
        <form class="layui-form" lay-filter="formType" method="post" name="myform" id="myform">
            
            <input type="hidden" name="data[id]" id="typeid" value="">
            <div class="layui-form-item">
                <label class="layui-form-label">分类名称</label>
                <div class="layui-input-inline">
                    <input type="text" name="data[name]" value="<?=$data['name'] ?? ''?>" required lay-verify="required" placeholder="" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">&nbsp;</label>
                <div class="layui-input-inline">
                    <button type="button" class="layui-btn layui-btn-blue" lay-submit="" lay-filter="typeSubmit">提交保存</button>
                </div>
            </div>
        </form>
    </div>
</body>

</html>
